package net.ib.mtalk.database.helper;

import net.ib.mtalk.database.DatabaseContract.GroupTable;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract.Groups;
import android.util.Log;

public class GroupDatabaseHelper {

	public static void insertGroup(Context context, String name) {
		ContentValues cv  = new ContentValues();
		cv.put(GroupTable.KEY_GROUP_NAME, name);
		
		ContentResolver cr = context.getContentResolver();
		cr.insert(GroupTable.CONTENT_URI, cv);
		Log.i("MTalk", "그룹 인서트 성공 : " + name);
	}
/*	public static void insert(Context context){
		String[] projection = {Groups._ID, Groups.GROUP_VISIBLE, Groups.DELETED, Groups.TITLE};
		String selection = Groups.GROUP_VISIBLE + "='1' AND " + Groups.DELETED + "='0'";
		String sortOrder = Groups._ID;
		Cursor cursor = context.getContentResolver().query(Groups.CONTENT_URI, projection, selection, null, sortOrder);
		ContentValues cv  = new ContentValues();
		try{
			while (cursor.moveToNext()) {
				cv.clear();
				cv.put(GroupTable.KEY_GROUP_NAME, cursor.getString(cursor.getColumnIndexOrThrow(Groups.TITLE)));
				ContentResolver cr = context.getContentResolver();
				cr.insert(GroupTable.CONTENT_URI, cv);
				Log.i("MTalk", "그룹 인서트 성공 : " + cursor.getString(cursor.getColumnIndexOrThrow(Groups.TITLE)));
			}
		} finally{
			cursor.close();
		}
	}
*/

	public static void deleteGroup(Context context, int id) {
		String where = GroupTable._ID + "=?";
		context.getContentResolver().delete(GroupTable.CONTENT_URI, where, new String[]{String.valueOf(id)});
	}

	public static void updateGroup(Context context, int groupId, String groupname) {
		ContentValues cv  = new ContentValues();
		cv.put(GroupTable.KEY_GROUP_NAME, groupname);
		String where = GroupTable._ID + "=?";
		String[] selectionArgs = {String.valueOf(groupId)};
		context.getContentResolver().update(GroupTable.CONTENT_URI, cv, where, selectionArgs);
		
	}

	public static void changeFold(Context context, int id, boolean isExpanded) {
		int fold = 0;
		if(isExpanded) fold = 0;
		else fold = 1;
		ContentValues cv  = new ContentValues();
		cv.put(GroupTable.KEY_GROUP_SET_FOLD, fold);
		String where = GroupTable._ID + "=?";
		String[] selectionArgs = {String.valueOf(id)};
		context.getContentResolver().update(GroupTable.CONTENT_URI, cv, where, selectionArgs);
	}
}
